#define _CRT_SECURE_NO_WARNINGS 1
#include"SqLnode.h"
void SqNodePrint(SNode* phead)
{
	SNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
SNode* BuySqNode(SqNodeType x)
{
	SNode* s = (SNode*)malloc(sizeof(SNode));
	s->data = x;
	s->next = NULL;
	return s;
}
void SqNodePushBack(SNode** pphead, SqNodeType x)
{
	SNode* s = BuySqNode(x);
	if (*pphead == NULL)
	{
		*pphead = s;
	}
	else
	{
		SNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = s;
		s->data = x;
		s->next = NULL;
	}
}
void SqNodePushFront(SNode** pphead, SqNodeType x)
{
	SNode* s = BuySqNode(x);
	s->next = *pphead;
	*pphead = s;
}
void SqNodePopFront(SNode** pphead)
{
	SNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}
void SqNodePopBack(SNode** pphead)
{
	SNode* tail = *pphead;
	SNode* prev = NULL;
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
		
	}
	else
	{
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		prev->next = NULL;
	}
}
SNode* SqNodeFind(SNode* phead, SqNodeType x)
{
	SNode* cur = phead;
	while (cur != NULL)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SqNodeInsert(SNode** pphead, SNode* pos, SqNodeType x)
{
	SNode* prev = *pphead;
	if (pos == *pphead)
	{
		SqNodePushFront(pphead, x);
	}
	else
	{
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SNode* s = BuySqNode(x);
		prev->next = s;
		s->next = pos;
	}
}
void SqNodeErase(SNode** pphead, SNode* pos)
{
	SNode* prev = *pphead;
	if (pos == *pphead)
	{
		SqNodePopFront(pphead);
	}
	else
	{
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
	
}